//package org.jetlinks.pro.elastic.search;
//
//import org.jetlinks.pro.elastic.search.configuration.ElasticSearchConfiguration;
//import org.jetlinks.pro.elastic.search.embedded.EmbeddedElasticSearchProperties;
//import org.jetlinks.pro.elastic.search.index.DefaultElasticSearchIndexManager;
//import org.jetlinks.pro.elastic.search.index.ElasticSearchIndexMetadata;
//import org.jetlinks.pro.elastic.search.index.ElasticSearchIndexProperties;
//import org.jetlinks.pro.elastic.search.index.strategies.DirectElasticSearchIndexStrategy;
//import org.jetlinks.pro.elastic.search.service.reactive.ReactiveElasticSearchService;
//import org.jetlinks.pro.elastic.search.service.reactive.ReactiveElasticsearchClient;
//import org.jetlinks.pro.test.utils.ContainerUtils;
//import org.springframework.data.elasticsearch.client.ClientConfiguration;
//import org.springframework.util.unit.DataSize;
//import org.springframework.web.reactive.function.client.ExchangeStrategies;
//import org.testcontainers.containers.GenericContainer;
//import reactor.core.publisher.Flux;
//
//import java.net.InetSocketAddress;
//import java.util.Collections;
//import java.util.Map;
//import java.util.concurrent.ConcurrentHashMap;
//
//public class ReactiveElasticsearchUtils {
//
//    static final Map<String, GenericContainer<?>> cached = new ConcurrentHashMap<>();
//
//
//    public static ReactiveElasticsearchClient createClient(String version) {
//
//        GenericContainer<?> container = cached.computeIfAbsent(version, (v) -> {
//            GenericContainer<?> container_ = ContainerUtils.newElasticSearch(v);
//            container_.start();
//            return container_;
//        });
//        ElasticSearchConfiguration configuration = new ElasticSearchConfiguration();
//
//        ClientConfiguration clientConfiguration = ClientConfiguration
//            .builder()
//            .connectedTo(InetSocketAddress.createUnresolved("127.0.0.1", container.getMappedPort(9200)))
//            .withWebClientConfigurer((webClient) -> {
//                ExchangeStrategies exchangeStrategies = ExchangeStrategies
//                    .builder()
//                    .codecs((configurer) -> configurer
//                        .defaultCodecs()
//                        .maxInMemorySize((int) DataSize.ofMegabytes(100).toBytes()))
//                    .build();
//                return webClient.mutate().exchangeStrategies(exchangeStrategies).build();
//            })
//            .build();
//
//        return configuration.defaultReactiveElasticsearchClient(new EmbeddedElasticSearchProperties(), clientConfiguration);
//    }
//
//    static {
//        System.setProperty("elasticsearch.buffer.timeout", "1");
//        System.setProperty("elasticsearch.buffer.size", "1000");
//    }
//
//    public static ReactiveElasticSearchService createService(String version, ElasticSearchIndexMetadata... metadata) {
//        ReactiveElasticsearchClient client = ReactiveElasticsearchUtils.createClient(version);
//        ElasticSearchIndexProperties properties = new ElasticSearchIndexProperties();
//        DefaultElasticSearchIndexManager indexManager = new DefaultElasticSearchIndexManager(
//            Collections.singletonList(new DirectElasticSearchIndexStrategy(client, properties)),
//            Collections.emptyList()
//        );
//        indexManager.setDefaultStrategy(DirectElasticSearchIndexStrategy.ID);
//        ReactiveElasticSearchService searchService = new ReactiveElasticSearchService(client, indexManager);
//        searchService.getBuffer().setRefreshWhenWrite(true);
//        Flux.just(metadata)
//            .flatMap(indexManager::putIndex)
//            .blockLast();
//        return searchService;
//    }
//}
